package com.youlai.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.system.common.result.PageResult;
import com.youlai.system.mapper.OrderMapper;
import com.youlai.system.mapper.ProductCategoryMapper;
import com.youlai.system.model.dto.OrderByCategoryTotalDTO;
import com.youlai.system.model.dto.OrderDto;
import com.youlai.system.model.dto.OrderTotalDto;
import com.youlai.system.model.entity.Order;
import com.youlai.system.model.vo.*;
import com.youlai.system.service.OrderService;
import jodd.util.StringUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cai
 * @create 2024-03-11 20:51
 */
@Service
@RequiredArgsConstructor // 会生成一个包含常量，所有已声明字段，并以字段名为参数的构造函数
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ProductCategoryMapper productCategoryMapper;
    @Override
    public PageResult<OrderVO> getPage(OrderDto orderDto) {
        int pageNum = orderDto.getPageNum();
        int pageSize = orderDto.getPageSize();
        String recvName = orderDto.getRecvName();
        Integer status = orderDto.getStatus();
        String startTime = orderDto.getStartTime();
        String endTime = orderDto.getEndTime();

        Page<Order> page = this.page(
                new Page<>(pageNum, pageSize),
                new LambdaQueryWrapper<Order>()
                        .like(StringUtil.isNotBlank(recvName), Order::getRecvName, recvName)
                        .eq(status != null, Order::getStatus, status)
                        .ge(StringUtil.isNotBlank(startTime), Order::getCreatedTime, startTime)
                        .le(StringUtil.isNotBlank(endTime), Order::getCreatedTime, endTime)
        );

        List<OrderVO> orderVOList = new ArrayList<>();
        page.getRecords().forEach(order -> {
            OrderVO orderVO = new OrderVO();
            BeanUtils.copyProperties(order,orderVO);
            orderVOList.add(orderVO);
        });

        return PageResult.success(orderVOList,page.getTotal());
    }

    /**
     * 商品总订单统计
     * @param orderTotalDto
     * @return
     */
    @Override
    public List<OrderTotalVO> getTotal(OrderTotalDto orderTotalDto) {
        return orderMapper.getTotal(orderTotalDto);
    }

    /**
     * 商品分类订单统计
     * @param orderByCategoryTotalDTO
     * @return
     */
    @Override
    public List<OrderByCategoryTotalVO> getCategoryTotal(OrderByCategoryTotalDTO orderByCategoryTotalDTO) {
        // 先获取其所有的分类，根据level
        List<CategoryPageVo> categoryByLevel = productCategoryMapper.getCategoryByLevel(orderByCategoryTotalDTO.getLevel());
        // 根据每个分类的id获取其订单总数
        List<OrderByCategoryTotalVO> orderByCategoryTotalVOS = new ArrayList<>();
        for (CategoryPageVo categoryPageVo : categoryByLevel) {
            System.out.println(categoryPageVo.getId()); //level=1,2,3
            OrderByCategoryTotalVO orderByCategoryTotalVO = new OrderByCategoryTotalVO();
            orderByCategoryTotalVO.setName(categoryPageVo.getName());
            // 获取每个分类的订单总数
            orderByCategoryTotalVO.setTotal(orderMapper.getCategoryTotal(orderByCategoryTotalDTO.getStartTime(), orderByCategoryTotalDTO.getEndTime(), categoryPageVo.getId(), orderByCategoryTotalDTO.getLevel()));
            orderByCategoryTotalVOS.add(orderByCategoryTotalVO);
        }
        // 排序，按照订单总数降序
        orderByCategoryTotalVOS.sort((o1, o2) -> o2.getTotal() - o1.getTotal());
        // 返回前10个
        return orderByCategoryTotalVOS.subList(0, 10);
    }

    @Override
    public List<OrderTotalStatisticsVO> getTotalStatistics(String type) {
        return orderMapper.getTotalStatistics(type);
    }
}
